package com.office.automate.dataaccess.impl.administration;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.office.automate.dataaccess.def.administration.PublicHolidayDao;
import com.office.automate.dataaccess.framework.DataAccessSessionFactory;
import com.office.automate.exception.administration.PublicHolidayExceptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.administration.PublicHoliday;

public class PublicHolidayDaoImplementer implements PublicHolidayDao {

	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	private static SessionFactory sessionFactory = null;
	private Session session = null;

	static {
		sessionFactory = DataAccessSessionFactory.getSessionFactory();
	}

	public PublicHolidayDaoImplementer() {
		session = sessionFactory.openSession();		
	}


	@Override
	public PublicHoliday persistHolidayDetails(PublicHoliday publicHoliday) throws FrontierVisibleException {
		try {
			Transaction transaction = session.beginTransaction();
			session.save(publicHoliday);
			transaction.commit();
		} catch(Exception e) {
			throw new FrontierVisibleException(PublicHolidayExceptionKeys.FAIL_TO_PERSIST_HOLIDAY, e);
		} finally {
			session.flush();
			session.close();
		}
		return publicHoliday;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PublicHoliday> getAvailablePublicHolidayPage(int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<PublicHoliday> aPublicHolidayList = null;
		try {
			 String SQL_QUERY ="from PublicHoliday publicHoliday order by publicHoliday.holidayDate desc";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 queryResult.setFirstResult(aStartingIndex);
			 queryResult.setMaxResults(aNumberOfRecords);
			 aPublicHolidayList = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(PublicHolidayExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return aPublicHolidayList;
	}


	@Override
	public int getAvailablePublicHolidayRecordCount() throws FrontierVisibleException {
		int recordCount = 0;
		try {
			 String SQL_QUERY ="select count(*) from PublicHoliday publicHoliday";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 recordCount = ((Long) queryResult.list().get(0)).intValue();
		} catch(Exception e) {
			throw new FrontierVisibleException(PublicHolidayExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		}
		return recordCount;
	}


	@Override
	public void deletePublicHoliday(PublicHoliday publicHoliday) throws FrontierVisibleException {
		try {
			session.beginTransaction();   
			session.delete(publicHoliday);  
			session.getTransaction().commit();  
		} catch(Exception e) {
			throw new FrontierVisibleException(PublicHolidayExceptionKeys.ERROR_OCCORRED_DURING_DELETION, e);
		} finally {
			session.flush();
			session.close();
		}
	}

	@Override
	public int getAvailablePublicHolidayRecordCountForDateRange(Date aFromDate, Date aToDate) throws FrontierVisibleException {
		int recordCount = 0;
		try {
			 String SQL_QUERY ="select count(*) from PublicHoliday publicHoliday where publicHoliday.holidayDate between to_date(:FromDate,'YYYY-MM-DD') and to_date(:ToDate,'YYYY-MM-DD')";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 queryResult.setParameter("FromDate", formatter.format(aFromDate));
			 queryResult.setParameter("ToDate", formatter.format(aToDate));
			 recordCount = ((Long) queryResult.list().get(0)).intValue();
		} catch(Exception e) {
			throw new FrontierVisibleException(PublicHolidayExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		}
		return recordCount;
	}
}
